/** * 日历组件逻辑 */
import CalendarData from './calendarData'

/**
 * dialog 组件实例
 * @type { WechatMiniprogram.Component.TrivialInstance & { show:()=>void, hide:() => {} } }
 * @p
 */
let dialogComponent = null
const calendarData = new CalendarData()

/**
 * 当前选择的日期
 * @type { Date }
 */
let currentDate = null

/**
 * 当前显示月份
 * @type { Date }
 */
let currentMonth = null

Component({
    properties: {
        /**
         * 日期格式字符串/时间戳
         * * 2021-04-21
         * * 12193717321
         */
        date: {
            type: String | Number,
            value: null
        },
        type: {   //one单日,range日期区间
            type: String,
            value: 'one'
        },
        minDate: {
            type: String | Number,
            value: null
        },
        maxDate: {
            type: String | Number,
            value: null
        },
        defaultDate: {  //默认渲染的开始时间
            type: Number | [],
            value: null
        }


    },

    data: {
        // 日历数据
        calendarData: [],

        // 当前月份信息数据
        currentMonthInfo: {},

        // 当前选中日期信息数据
        currentDateInfo: {},

    },

    observers: {
        date: function (data) {
            console.log('data111', data);
            currentDate = new Date(data)
            currentMonth = new Date(currentDate)
            this.setData({
                currentDateInfo: calendarData.dateInfo(currentDate),
            })
            this.getData(currentDate)
        }
    },

    lifetimes: {
        attached: function () {
            dialogComponent = this.selectComponent('#dialog')
        },
    },

    methods: {
        // 显示弹出层
        // 重制月份数据
        // 重制当前日期信息
        // 重制月份日历
        // 重制月份信息
        show: function () {
            dialogComponent.show()
            currentMonth = new Date(currentDate)
            this.setData({
                currentDateInfo: calendarData.dateInfo(currentDate),
            })
            this.getData(currentDate)
        },

        // 获取月份日历-更新月份信息
        getData: function (date) {
            calendarData.date = date
            this.setData({
                currentMonthInfo: calendarData.dateInfo(currentMonth),
                calendarData: calendarData.getData().data,
            })
        },
        plusMonth: function () {
            let year = currentMonth.getFullYear()
            let month = currentMonth.getMonth()
            let newDate = new Date(year, month + 1, 1)
            currentMonth = newDate
            this.getData(newDate)
        },

        reduceMonth: function () {
            let year = currentMonth.getFullYear()
            let month = currentMonth.getMonth()
            let newDate = new Date(year, month - 1, 1)
            currentMonth = newDate
            this.getData(newDate)
        },

        selectDay: function (event) {
            let { week, day } = event.currentTarget.dataset
            let date = this.data.calendarData[week][day]
            this.setData({
                currentDateInfo: calendarData.dateInfo(date.date),
            })
        },

        calendarCancel: function (event) {
            let { type } = event.currentTarget.dataset
            if (type) {
                this.triggerEvent('date-change', this.data.currentDateInfo)
            }
            dialogComponent.hide()
        }
    }
})
